﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/User.Master" AutoEventWireup="true" CodeBehind="ContactList.aspx.cs" Inherits="CRMWeb.Pages.Contact.ContactList" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript">
        $(document).ready(function () {
            $('#txtSearchValue').val('');

            var advSearchtext = getQuerystring('searchtext', '');
            if (advSearchtext == -1) {
                setCookie('LastSearchClient', '');
                advSearchtext = '';
            }
            else if (advSearchtext.length == 0)
                advSearchtext = getCookie('LastSearchClient')
            else
                setCookie('LastSearchClient', advSearchtext);

            var days = getQuerystring('days', '');

            if (days == 30)
                document.getElementById('rdb30days').checked = true;
            else if (days == 90)
                document.getElementById('rdb90days').checked = true;
            else if (days == "All" || days.length == 0)
                document.getElementById('rdbAll').checked = true;

            $('#dgAppContact').datagrid({
                url: '/WebServices/ApplicationContact.ashx?method=ApplicationContactSelectAll&searchtext=' + advSearchtext + '&days=' + days,
                title: ' All Clients ' + setPanelText(),
                onDblClickRow: function (index, rowdata) {
                    window.location.href = "/Pages/Contact/ContactDetail.aspx?contact_id=" + rowdata.ContactID;
                },
                onSortColumn: function (sort, order) {
                    quickSearch(sort + '  ' + order);
                },
                onSelect: function (rowIndex, rowData) {
                    document.onkeydown = function chekKey(e) {
                        e = e || window.event;
                        if (e.keyCode == '38') // up arrow
                        {
                            if (rowIndex == 0)
                                $('#dgAppContact').datagrid('selectRow', 29);
                            else
                                $('#dgAppContact').datagrid('selectRow', rowIndex - 1);
                        }
                        else if (e.keyCode == '40') // down arrow
                        {
                            if (rowIndex == 29)
                                $('#dgAppContact').datagrid('selectRow', 0);
                            else
                                $('#dgAppContact').datagrid('selectRow', rowIndex + 1);
                        }
                    }
                    document.onkeypress = function chekKey(e) {
                        e = e || window.event;
                        if (e.keyCode == '13') // Enter Key
                            window.location.href = "/Pages/Contact/ContactDetail.aspx?contact_id=" + rowData.ContactID;
                    }
                },
                onLoadSuccess: function (data) {
                    if (data.rows.length == 1) {
                        $('#divMsg').hide();
                        window.location.href = "/Pages/Contact/ContactDetail.aspx?contact_id=" + data.rows[0].ContactID;
                    }
                    else if (data.rows.length == 0) {
                        $('#divMsg').show();
                    }
                    else {
                        $('#divMsg').hide();
                    }
                }
            });
            fnLeftNavSelected('A12');
            $('#cmbSearchField').combobox('textbox').bind('click', function () {
                $('#cmbSearchField').combobox('showPanel');
            });
            $('#cmbSearchOperator').combobox('textbox').bind('click', function () {
                $('#cmbSearchOperator').combobox('showPanel');
            });
        });
        document.onkeydown = function chekKey(e) {
            e = e || window.event;
            if (e.keyCode == '38') // up arrow
            {
                $('#dgAppContact').datagrid('selectRow', 29);
            }
            else if (e.keyCode == '40') // down arrow
            {
                $('#dgAppContact').datagrid('selectRow', 0);
            }
        }

        var isString_re = /^[a-zA-Z]*$/;
        function isString(s) {
            var s = String(s).search(isString_re) != -1
            if (s == true) {
                return 0;
            }
        }
        var isWhole_re = /^(?=.*\d)(?:[\d ]+)$/;
        function isNumber(s) {
            var s = String(s).search(isWhole_re) != -1
            if (s == true) {
                return 1;
            }
        }
        var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
        function isEmail(s) {
            var s = String(s).search(isEmail_re) != -1;
            if (s == true) {
                return 2;
            }
        }

        function formattedMobileNo(originalNumber) {
            var len = originalNumber.length;
            var retunNum = '';
            if (originalNumber.search(" ") >= 0)
                retunNum = originalNumber;
            else {
                if (len == 10)
                    retunNum = originalNumber.substring(0, 4) + " " + originalNumber.substring(4, 7) + " " + originalNumber.substring(7, 10);
                else if (len > 4 && len <= 7)
                    retunNum = originalNumber.substring(0, 4) + " " + originalNumber.substring(4, len);
                else if (len > 7 && len < 10)
                    retunNum = originalNumber.substring(0, 4) + " " + originalNumber.substring(4, 7) + " " + originalNumber.substring(7, len);
                else if (len <= 4 || len > 10)
                    retunNum = originalNumber;
            }
            return retunNum;
        }
        function setPanelText() {
            var str;
            var cookieText = getCookie('LastSearchClient');
            if ((cookieText == '') && ($('#txtSearchValue').val() == ''))
                str = '';
            else if ($('#txtSearchValue').val() != '' && cookieText == '')
                str = '( ' + $('#cmbSearchField').combobox('getText') + ' ' + $('#cmbSearchOperator').combobox('getText') + ' ' + $('#txtSearchValue').val() + ')';
            else if ($('#txtSearchValue').val() == '' && cookieText != '') {
                if (isNumber(cookieText) == 1)
                    str = '(Mobile=' + cookieText + ')';
                else if (isEmail(cookieText) == 2)
                    str = '(Email=' + cookieText + ')';
                else if (isString(cookieText) == 0)
                    str = '(SurName=' + cookieText + ')';
                else
                    str = '(FullName=' + cookieText + ')';
            }
            else if ((cookieText != '') && ($('#txtSearchValue').val() != '')) {
                if (isNumber(cookieText) == 1)
                    str = '(Mobile=' + cookieText;
                else if (isEmail(cookieText) == 2)
                    str = '(Email=' + cookieText;
                else if (isString(cookieText) == 0)
                    str = '(SurName=' + cookieText;
                else
                    str = '(FullName=' + cookieText;
                str += ', ' + $('#cmbSearchField').combobox('getText') + ' ' + $('#cmbSearchOperator').combobox('getText') + ' ' + $('#txtSearchValue').val() + ')';
            }
            str = str.replace(/%20/g, ' ');
            return str;
        }
        function quickSearch(Sorting) {
            var txtSearchValue = '';
            var cmbSearchField = $('#cmbSearchField').combobox('getValue');
            var cmbSearchOperator = $('#cmbSearchOperator').combobox('getValue');

            if (cmbSearchField == 'DateBirth') {
                txtSearchValue = DefaultformatDate($('#txtDateBirth').val());
            }
            else
                txtSearchValue = $('#txtSearchValue').val();

            if (cmbSearchField == "Mobile")
                txtSearchValue = formattedMobileNo($('#txtSearchValue').val());

            txtSearchValue = txtSearchValue.trim();
            if (cmbSearchField == 'Email') {
                var strcondition = ' and (';
                if (cmbSearchOperator == ' like ' || cmbSearchOperator == '  like  ') {
                    if (cmbSearchOperator == '  like  ')
                        strcondition += cmbSearchField + cmbSearchOperator + '\'' + txtSearchValue + '%\'' + ' OR secondEmailAddress ' + cmbSearchOperator + '\'' + txtSearchValue + '%\'';
                    else
                        strcondition += cmbSearchField + cmbSearchOperator + '\'' + txtSearchValue + '%\'' + ' OR secondEmailAddress ' + cmbSearchOperator + '\'' + txtSearchValue + '%\'';
                }
                else
                    strcondition += cmbSearchField + cmbSearchOperator + '\'' + txtSearchValue + '\'' + ' OR secondEmailAddress ' + cmbSearchOperator + '\'' + txtSearchValue + '\'';
                strcondition += ')';
            }
            else {
                var strcondition = ' and ';
                if (cmbSearchOperator == ' like ' || cmbSearchOperator == '  like  ') {
                    if (cmbSearchOperator == '  like  ')
                        strcondition += cmbSearchField + cmbSearchOperator + '\'' + txtSearchValue + '%\'';
                    else
                        strcondition += cmbSearchField + cmbSearchOperator + '\'' + txtSearchValue + '%\'';
                }
                else
                    strcondition += cmbSearchField + cmbSearchOperator + '\'' + txtSearchValue + '\'';
            }

            var days = getQuerystring('days', '');

            $('#dgAppContact').datagrid({
                title: 'All Clients ' + setPanelText(),
                url: '/WebServices/ApplicationContact.ashx?method=ApplicationContactSelectAll&searchtext=' + getCookie('LastSearchClient') + '&conditiontext=' + strcondition + '&sortCondition=' + Sorting + '&days=' + days

            });
        }
        function PopupWindowCenter(URL, title, w, h) {
            var left = (screen.width / 2) - (w / 2);
            var top = (screen.height / 2) - (h / 2);
            var strfeatures = 'width=' + w + ', height= ' + h + ', top=' + top + ',left=' + left;
            var newWin = window.open(URL, title, strfeatures);
        }
        function OpenDetective() {
            var newTab = window.open('/Pages/Contact/DuplicateContact.aspx', 'blank');
        }
        function DefaultformatDate(value) {
            if (value == '')
                return '';
            else {
                dArr = value.split('/');
                return dArr[2] + '-' + dArr[1] + '-' + dArr[0];
            }
        }
        function formatDate(value, row) {
            if (result = false)
                return '';
            else {
                var date = null;
                if (value == null)
                    date = new Date();
                else
                    date = new Date(value);
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                var month = '';
                switch (m) {
                    case 1:
                        month = 'Jan';
                        break;
                    case 2:
                        month = 'Feb';
                        break;
                    case 3:
                        month = 'Mar';
                        break;
                    case 4:
                        month = 'Apr';
                        break;
                    case 5:
                        month = 'May';
                        break;
                    case 6:
                        month = 'Jun';
                        break;
                    case 7:
                        month = 'Jul';
                        break;
                    case 8:
                        month = 'Aug';
                        break;
                    case 9:
                        month = 'Sep';
                        break;
                    case 10:
                        month = 'Oct';
                        break;
                    case 11:
                        month = 'Nov';
                        break;
                    case 12:
                        month = 'Dec';
                        break;
                }
                var d = date.getDate();
                return (d < 10 ? ('0' + d) : d) + ' ' + month + ' ' + y;
            }
        }
        function formatContactProfile(value, row) {
            return "<a href='/Pages/Contact/ContactDetail.aspx?contact_id=" + row.ContactID + "' target='_blank' style='cursor:pointer'>View</a>";
        }
        function formatDateTime(value, row) {
            if (result = false)
                return '';
            else {
                var date = null;
                if (value == null)
                    date = new Date();
                else
                    date = new Date(value);
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                var month = '';
                switch (m) {
                    case 1:
                        month = 'Jan';
                        break;
                    case 2:
                        month = 'Feb';
                        break;
                    case 3:
                        month = 'Mar';
                        break;
                    case 4:
                        month = 'Apr';
                        break;
                    case 5:
                        month = 'May';
                        break;
                    case 6:
                        month = 'Jun';
                        break;
                    case 7:
                        month = 'Jul';
                        break;
                    case 8:
                        month = 'Aug';
                        break;
                    case 9:
                        month = 'Sep';
                        break;
                    case 10:
                        month = 'Oct';
                        break;
                    case 11:
                        month = 'Nov';
                        break;
                    case 12:
                        month = 'Dec';
                        break;
                }
                var d = date.getDate();
                return (d < 10 ? ('0' + d) : d) + ' ' + month + ' ' + y + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
            }
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div id="divDataGrid">
        <table id="dgAppContact" title="All Clients" style="width: auto;" autorowheight="true" pagination="true" data-options="pageSize:30,toolbar:'#tbContactList'"
            rownumbers="true" fitcolumns="true" singleselect="true" remotesort="true">
            <thead>
                <tr>
                    <th field="ContactID" width="20" sortable="true">Client No</th>
                    <th field="ClientName" width="80" sortable="true">Full Name</th>
                    <th field="DateBirth" width="40" align="middle" data-options="formatter:formatDate" sortable="true">Birth Date</th>
                    <th field="State" width="30" align="middle" sortable="true">Region</th>
                    <th field="CreateDate" width="50" sortable="true">Create Date</th>
                    <th field="EditDate" width="50" sortable="true">Edit Date</th>
                    <th field="ContactProfileid" width="20" data-options="formatter:formatContactProfile">Profile</th>
                </tr>
            </thead>
        </table>
    </div>
    <div id="tbContactList" style="padding: 5px; height: auto">
        Field: 
            <select id="cmbSearchField" class="easyui-combobox" panelheight="auto" style="width: 100px"
                data-options="onSelect:function(){
                if($('#cmbSearchField').combobox('getValue')=='DateBirth'){
                        $('#txtDateBirth').val('');
                        $('#txtDateBirth').mask('99/99/9999');
                        $('#spnDate').show();
                $('#txtSearchValue').hide();
                    }
                    else
                    {
                        $('#spnDate').hide();
                        $('#txtDateBirth').unmask();
                        $('#txtDateBirth').val('');
                        $('#txtSearchValue').show();
                        $('#txtSearchValue').val('');
                    }
                }">
                <option value="Email">Email</option>
                <option value="ContactID">Client No</option>
                <option value="FirstName">First Name</option>
                <option value="SurName">Surname</option>
                <option value="Mobile">Mobile</option>
                <option value="HomePhone">HomePhone</option>
                <option value="State">State</option>
                <option value="DateBirth">Birthdate</option>
            </select>
        Operator: 
            <select id="cmbSearchOperator" class="easyui-combobox" panelheight="auto" style="width: 100px">
                <option value="  like  ">Start With</option>
                <option value=" like ">Contains</option>
                <option value="=">Equals</option>
            </select>
        Value:
        <input id="txtSearchValue" class="easyui-validatebox" />
        &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
        <span id="spnDate" style="display: none">
            <input id="txtDateBirth" type="text" style="font-family: Verdana; font-size: 12px" />
        </span>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-search" onclick="quickSearch('')"></a>
        <a id="lnkaddClient" href="/Pages/Contact/ContactDetail.aspx" class="easyui-linkbutton" iconcls="icon-add">New Client</a>
        <a style="display: none" id="lnkdetective" class="easyui-linkbutton" iconcls="icon-cancel" onclick="OpenDetective();">Detective Dup</a>
        <div id="divMsg" style="display: none; width: 100%; height: 30px; border: solid; font-family: Cambria; font-size: medium; border-color: mistyrose; color: red; vertical-align: middle; text-align: center">
            Didn't get any client match this condition
        </div>
    </div>
    <script type="text/javascript">
        fnLeftNavSelected('menu_appdetail');
    </script>
</asp:Content>
